import 'package:base_flutter/base_flutter.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/a_yyt/models/dynamic_entity.dart';
// import 'package:ls_work/a_elder/models/elder_entity.dart';
// import 'package:ls_work/a_yyt/models/old_life_entity.dart';
import 'package:ls_work/a_yyt/presenter/old_life_presenter.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/styles.dart';
import 'package:ls_work/a_yyt/widget/custom_dynamic_cell.dart';
import 'package:ls_work/mvp/base_page_state.dart';
import 'package:ls_work/provider/base_list_provider.dart';
import 'package:ls_work/widgets/my_refresh_divider_list.dart';
// import 'package:ls_work/widgets/my_refresh_list.dart';
import 'package:ls_work/widgets/state_layout.dart';
import 'package:provider/provider.dart';

import '../../util/flat_button.dart';



class HomeOldLifePage extends StatefulWidget {
  @override
  HomeOldLifePageState createState() => HomeOldLifePageState();
}

class HomeOldLifePageState
    extends BasePageState<HomeOldLifePage, OldLifePresenter>
    with AutomaticKeepAliveClientMixin {
  BaseListProvider<DynamicInfo> provider = BaseListProvider<DynamicInfo>();

  int status = 0;
  int _page = 1;
  String _statusName = '待审核';

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    /// 默认为加载中状态
    provider.setStateType(StateType.empty);
    _page = 1;

    WidgetsBinding.instance.addPostFrameCallback((_) {
      /// 接口请求
      print(presenter);
      presenter.fectData(status, _page, true);
    });
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ChangeNotifierProvider<BaseListProvider<DynamicInfo>>(
      create: (_) => provider,
      child: Scaffold(
        appBar: AppBar(
          title: Text('长者生活'),
          elevation: 0.5,
        ),
        body: Consumer<BaseListProvider<DynamicInfo>>(
          builder: (_, provider, __) {
            return DeerSeparatedListView(
              key: Key('OldLife_list'),
              itemCount: provider.list.length + 1,
              stateType: provider.stateType,
              onRefresh: _onRefresh,
              loadMore: _loadMore,
              hasMore: provider.hasMore,
              itemBuilder: (_, index) {
                switch (index) {
                  case 0:
                    return Container(
                      width: double.infinity,
                      height: 50,
                      child: FlatsButton(
                        child: Row(
                          children: [
                            Icon(
                              Icons.arrow_drop_down,
                              color: Colours.text_gray,
                              size: 30,
                            ),
                            Text(_statusName, style: TextStyles.textGray18),
                          ],
                        ),
                        onPressed: _showBottomSheet,
                      ),
                    );
                    break;
                  default:
                    return DynamicCell(info: provider.list[index - 1]); //
                }
              },
              separatorBuilder: (BuildContext context, int index) {
                switch (index) {
                  case 0:
                    return Container(
                      width: double.infinity,
                      height: 1,
                      color: Colours.bg_gray,
                    );
                    break;
                  default:
                    return Container(
                      color: Colours.bg_color,
                      height: 5,
                    );
                }
              },
            );
          },
        ),
      ),
    );
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.fectData(status, _page, false);
  }

  Future _loadMore() async {
    _page++;
    await presenter.fectData(status, _page, false);
  }

  @override
  OldLifePresenter createPresenter() {
    return OldLifePresenter();
  }

  ///底部弹窗
  _showBottomSheet() {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            SizedBox(
              height: 20,
            ),
            Text(
              '请选择',
              style: TextStyles.textGray16,
            ),
            SizedBox(
              height: 10,
            ),
            Divider(),
            GestureDetector(
              behavior: HitTestBehavior.translucent,
              onTap: () {
                setState(() {
                  _statusName = '待审核';
                  status = 0;
                  _onRefresh();
                  Navigator.pop(context);
                });
              },
              child: Container(
                alignment: Alignment.center,
                width: ScreenUtil.getScreenW(context),
                padding: EdgeInsets.all(10),
                child: Text('待审核', style: TextStyles.textGray16),
              ),
            ),
            Divider(),
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                setState(() {
                  _statusName = '通过';
                  status = 1;
                  _onRefresh();
                  Navigator.pop(context);
                });
              },
              child: Container(
                alignment: Alignment.center,
                width: ScreenUtil.getScreenW(context),
                padding: EdgeInsets.all(10),
                child: Text('通过', style: TextStyles.textGray16),
              ),
            ),
            Divider(),
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                setState(() {
                  _statusName = '失败';
                  status = 2;
                  _onRefresh();
                  Navigator.pop(context);
                });
              },
              child: Container(
                alignment: Alignment.center,
                width: ScreenUtil.getScreenW(context),
                padding: EdgeInsets.all(10),
                child: Text('失败', style: TextStyles.textGray16),
              ),
            ),
          ],
        );
      },
    );
  }
}
